﻿using DSFramework;

using Quartz;

using System;
using System.Collections.Generic;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading.Tasks;

namespace GetWebsite.Blogs.Blogs
{
    public class SendMailJob : IJob
    {
        static SendMailJob()
        {
            if (mailConfig == null)
            {
                mailConfig = ConfigHelper.ReadEmailConfigJson();
            }
            if (cnblog == null)
            {
                cnblog = new CnBlog();
            }
        }
        static MailConfig mailConfig = null;
        static CnBlog cnblog = null;
        public async Task Execute(IJobExecutionContext context)
        {
            await Task.Run(async () =>
            {
                //每隔 15 分钟  自动查找网页最新的文章，并根据数据自动推送
                await Console.Out.WriteLineAsync("你好世界");
                using (Ping ping = new Ping())
                {
                    PingReply pingReply = ping.Send("www.bing.com");
                    if (pingReply.Status == IPStatus.Success)
                    {
                        await Console.Out.WriteLineAsync($"定时获取博客 ：{DateTime.Now:yyyy-MM-dd HH:mm:ss}\n缓存的博客数量：{CnBlog.caches.Count}\n\n");
                        cnblog.GetAndSendBlogs(mailConfig);
                    }
                    else
                    {
                        await Console.Out.WriteLineAsync("当前网络不通畅!");
                    }
                }
            });
        }
    }
}
